package scrapper;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PixivScrapper extends AbstractScrapper {

	public PixivScrapper(String id, String agent) throws IOException {
		super(id, agent);
	}

	@Override
	public void login(String username, String password) throws IOException {}

	@Override
	public String buildURL(String id) { return "http://www.pixiv.com/works/"+id; }

	@Override
	public boolean buildPage() {
		Pattern pattern;
		Matcher matcher;
		String rawTitle = getPage().select("meta[property=og:title]").get(0).attr("content");
		String rawDesc = getPage().select("meta[property=og:description]").get(0).attr("content");
		String rawTags = getPage().select("meta[name=keywords]").get(0).attr("content");
		String rawDate = getPage().select("span[class=data with-icon]").get(0).attr("title");

		pattern = Pattern.compile("( by )");
		matcher = pattern.matcher(rawTitle);
		matcher.find();

		setTitle(rawTitle.substring(0, matcher.start()));
		setAuthor(rawTitle.substring(matcher.end(), rawTitle.length()));
		setDate(rawDate);
		setDesc(rawDesc);
		setTags(rawTags.split(","));
		
		return true;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			PixivScrapper p1 = new PixivScrapper("26756932",
					"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0");
			p1.connect();
			p1.buildPage();

			System.out.println(p1);
			System.out.println(p1.getDesc());
			for(String tag : p1.getTags())
				System.out.println(tag);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
